package com.example.cloudmaps;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.StrictMode;

public class InterestPlaces {
	private String requesturl = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?radius=5000&sensor=false&key=AIzaSyCcwckNKdWeYORy7WF8l3ktnOmuuwct7qc&location=";
	private static final String  APIKEY = "AIzaSyCcwckNKdWeYORy7WF8l3ktnOmuuwct7qc";

	public List<MapPlaces> nearbyPlaces(double lat, double lng, String interest){
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        
        List<MapPlaces> placesList = new ArrayList<MapPlaces>();
        MapPlaces mapPlaces;
        
        //interest = "food|types=cafe";
        
        String combine = lat+","+lng;
        requesturl = requesturl+""+combine;
        requesturl = requesturl+""+"&types="+interest;
        
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet req = new HttpGet(requesturl);
        
        try{
        	HttpResponse res = client.execute(req);
			HttpEntity jasonentity = res.getEntity();
			InputStream in = jasonentity.getContent();
			JSONObject jsonobj = new JSONObject(convertStreamToString(in));
			JSONArray resarray = jsonobj.getJSONArray("results");
			
			if(resarray.length() == 0){
				//No Results
				System.out.println("No Results");
			}else{
				for(int j=0; j<resarray.length(); j++){
					mapPlaces = new MapPlaces();
					
					mapPlaces.setLng(((JSONArray) jsonobj.get("results")).getJSONObject(j).getJSONObject("geometry").getJSONObject("location").getString("lng"));
					mapPlaces.setLat(((JSONArray) jsonobj.get("results")).getJSONObject(j).getJSONObject("geometry").getJSONObject("location").getString("lat"));
					mapPlaces.setName(resarray.getJSONObject(j).getString("name"));
					mapPlaces.setAddr(resarray.getJSONObject(j).getString("vicinity"));
					mapPlaces.setRef(resarray.getJSONObject(j).getString("reference"));
					
					placesList.add(mapPlaces);
				}//end of For Loop
				
			}//End of else block
			
        }catch (ClientProtocolException e) {
        	e.printStackTrace();
        }catch (IOException e) {
        	e.printStackTrace();
        }catch (Exception e){
        	e.printStackTrace();
        }//end of the catches and Try block
        
        return placesList;
        
	}//end of nearbyplaces method
	
	private String convertStreamToString(InputStream in) {
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		StringBuilder jsonstr = new StringBuilder();
		String line;
		try{
			while((line = br.readLine()) != null)
			{
				String t = line+"\n";
				jsonstr.append(t);
			}
			br.close();
		}catch (IOException e) {
			e.printStackTrace();
		}
		return jsonstr.toString();
	}
}
